//
//  NearListTableViewCell.m
//  HJZWeiBo
//
//  Created by lanou on 16/5/30.
//  Copyright © 2016年 蒋晨成. All rights reserved.
//

#import "NearListTableViewCell.h"
#import <UIImageView+WebCache.h>
@implementation NearListTableViewCell

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.userImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 30, 30)];
        self.userImageView.layer.cornerRadius = 15;
        [self.contentView addSubview:_userImageView];
        
        self.userName = [[UILabel alloc]initWithFrame:CGRectMake(50, 10, kWidth / 2, 20)];
        self.userName.font = [UIFont systemFontOfSize:15];
        [self.contentView addSubview:_userName];
        
        self.time = [[UILabel alloc]initWithFrame:CGRectMake(50, 30, 50, 15)];
        self.time.font = [UIFont systemFontOfSize:13];
        self.time.textColor = [UIColor grayColor];
        [self.contentView addSubview:_time];
        
        self.location = [[UILabel alloc]initWithFrame:CGRectMake(self.time.frame.origin.x + self.time.frame.size.width + 10, self.time.frame.origin.y, kWidth / 2, 15)];
        self.location.font = [UIFont systemFontOfSize:13];
        self.location.textColor = [UIColor grayColor];
        [self.contentView addSubview:_location];
        
        self.text = [[UILabel alloc]initWithFrame:CGRectMake(10, 45, kWidth - 20, 50)];
        self.text.font = [UIFont systemFontOfSize:15];
        self.text.numberOfLines = 0;
        [self.contentView addSubview:_text];
        
        self.zhuanfa = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.zhuanfa setTitle:[NSString stringWithFormat:@"%ld", (long)self.model.reposts_count] forState:UIControlStateNormal];
        
        self.pinglun = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.pinglun setTitle:[NSString stringWithFormat:@"%ld", self.model.comments_count] forState:UIControlStateNormal];
        
        self.zan = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.zan setTitle:[NSString stringWithFormat:@"%ld", self.model.attitudes_count] forState:UIControlStateNormal];
            self.picImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, self.text.frame.origin.y + self.text.frame.size.height + 10, kWidth / 2, kWidth / 2)];
        [self.contentView addSubview:_picImageView];
        
        self.zhuanfa.frame = CGRectMake(10, self.picImageView.frame.origin.y + self.picImageView.frame.size.height + 10, (kWidth - 20) / 3, 30);
        
        self.pinglun.frame = CGRectMake(10 + (kWidth - 20) / 3, self.picImageView.frame.origin.y + self.picImageView.frame.size.height + 10, (kWidth - 20) / 3, 30);
        
        self.zan.frame = CGRectMake(10 + 2 * (kWidth - 20) / 3, self.picImageView.frame.origin.y + self.picImageView.frame.size.height + 10, (kWidth - 20) / 3, 30);
        self.zhuanfa.backgroundColor = COLOR(230, 230, 230, 1);
        self.zhuanfa.layer.cornerRadius = 5;
        self.zan.backgroundColor = COLOR(230, 230, 230, 1);
        self.zan.layer.cornerRadius = 5;
        self.pinglun.backgroundColor = COLOR(230, 230, 230, 1);
        self.pinglun.layer.cornerRadius = 5;
        [self.contentView addSubview:_zhuanfa];
        [self.contentView addSubview:_zan];
        [self.contentView addSubview:_pinglun];
        
    }
    return self;
}

- (void)setModel:(NearListModel *)model{
    _model = model;
    [self.userImageView sd_setImageWithURL:[NSURL URLWithString:_model.user[@"profile_image_url"]]];
    self.userName.text = _model.user[@"name"];
    self.location.text = _model.source;
    self.time.text = [self resolveSinaWeiboDate:_model.user[@"created_at"]];
    self.text.text = _model.text;
    [self.picImageView sd_setImageWithURL:[NSURL URLWithString:_model.thumbnail_pic]];
    
    CGFloat textHeight = [_text.text boundingRectWithSize:CGSizeMake(kWidth - 20, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil].size.height;
    CGRect frame = CGRectMake(10, 45, kWidth - 20, textHeight);
    if (_model.text == nil) {
        frame = CGRectMake(10, 45, 0, 0);
    }
    self.text.frame = frame;
    
    CGFloat timeWidth = [_time.text boundingRectWithSize:CGSizeMake(MAXFLOAT, 20) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]} context:nil].size.width;
    CGRect locationFrame = CGRectMake(self.time.frame.origin.x + timeWidth + 10, self.time.frame.origin.y, kWidth / 2, 15);
    self.location.frame = locationFrame;
    
    
    CGRect picFrame = CGRectMake(10, self.text.frame.origin.y + self.text.frame.size.height + 10, 100, 100);
    if (_model.thumbnail_pic == nil) {
        picFrame = CGRectMake(10, self.text.frame.origin.y + self.text.frame.size.height + 10, 0, 0);
    }
    self.picImageView.frame = picFrame;
    
    CGRect zhuanfaFrame = CGRectMake(10, picFrame.origin.y + picFrame.size.height + 10, (kWidth - 20) / 3, 30);
    self.zhuanfa.frame = zhuanfaFrame;
    [self.zhuanfa setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    CGRect pinglunFrame = CGRectMake(10 + (kWidth - 20) / 3, picFrame.origin.y + picFrame.size.height + 10, (kWidth - 20) / 3, 30);
    self.pinglun.frame = pinglunFrame;
    [self.pinglun setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    CGRect zanFrame = CGRectMake(10 + 2 * (kWidth - 20) / 3, picFrame.origin.y + picFrame.size.height + 10, (kWidth - 20) / 3, 30);
    self.zan.frame = zanFrame;
    [self.zan setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    if (_model.user[@"name"] == nil) {
        model.height = 0;
        zanFrame = CGRectMake(0, 0, 0, 0);
        self.zan.frame = zanFrame;
        self.pinglun.frame = zanFrame;
        self.zhuanfa.frame = zanFrame;
//        self.userName.frame = zanFrame;
//        self.time.frame = zanFrame;
//        self.location.frame = zanFrame;
//        self.userImageView.frame = zanFrame;
//        self.text.frame = zanFrame;
//        self.picImageView.frame = zaFrame;
    }
    model.height = zanFrame.origin.y + 35;
    
    
    
    
}

//解析新浪微博中的日期

- (NSString*)resolveSinaWeiboDate:(NSString*)date{
    NSDateFormatter *iosDateFormater=[[NSDateFormatter alloc]init];
    iosDateFormater.dateFormat=@"EEE MMM d HH:mm:ss Z yyyy";
    
    iosDateFormater.locale=[[NSLocale alloc]initWithLocaleIdentifier:@"en_US"];
    NSDate *inputDate=[iosDateFormater dateFromString:date];
    
    //目的格式
    NSDateFormatter *resultFormatter=[[NSDateFormatter alloc]init];
    //[resultFormatter setDateFormat:@"YY-MM-dd HH:mm"];
    [resultFormatter setDateFormat:@"HH:mm"];
    return [resultFormatter stringFromDate:inputDate];
}

@end
